
package com.dotools.ui;

import java.lang.ref.SoftReference;
import java.util.LinkedList;

import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.dotools.base.CommonConstants;
import com.dotools.debug.LOG;
import com.dotools.utils.DevicesUtils;

public class ViewCacheEntry {
    private static final boolean LOGV = CommonConstants.IS_DEBUG && false;

    private static final int MAX_POOLED_HINT = 32;
    private static LinkedList<ViewCacheEntry> sPool = new LinkedList<ViewCacheEntry>();

    static boolean sConfigLowMemoryMode = false;

    public boolean inUse;

    public ViewGroup parentView;
    public ViewGroup.LayoutParams layout;
    public int seq;
    public int size;

    View view;
    SoftReference<Bitmap> bitmapRef;
    Bitmap bitmap;

    private ViewCacheEntry() {
        // disable new from outside
    }

    public static ViewCacheEntry obtain(View key) {
        ViewCacheEntry vci = null;

        synchronized (sPool) {
            if (sPool.size() > 0) {
                vci = sPool.get(0);
                sPool.remove(0);
            }
        }

        if (vci == null) {
            vci = new ViewCacheEntry();
        }

        vci.view = key;
        vci.parentView = (ViewGroup) key.getParent(); // normally, it is CellLayout
        vci.layout = key.getLayoutParams();
        vci.bitmapRef = null; // do not generate bitmap here, should distribute into individual draw
        vci.seq = -1;
        vci.inUse = true;
        vci.size = key.getWidth() * key.getHeight();
        if (vci.size <= 0) {
            vci.size = 1;
        }

        if (LOGV) {
            if (!key.getClass().getSimpleName().equals("BubbleTextView")) {
                // LOG.logI("Creating cache for view: " + key.toString(), new Exception());
                LOG.logI("Creating cache for view: " + key.toString());
            }
        }

        return vci;
    }

    public void recycle() {
        inUse = false;

        if (LOGV) {
            if (!view.getClass().getSimpleName().equals("BubbleTextView")) {
                //LOG.logI("destroy cache for view: " + view.toString(), new Exception());
                LOG.logI("destroy cache for view: " + view.toString());
            }
        }

        // drop drawing cache
        if (view != null) {
            if (view instanceof IDropCache) {
                // drop without destroy
                ((IDropCache) view).dropCache();
                // LOG.logI("dropCache: " + view);
            } else {
                view.destroyDrawingCache();
                // LOG.logI("destroyDrawingCache: " + view);
            }
        }

        bitmapRef = null;
        bitmap = null;
        view = null;
        layout = null;
        seq = -1;

        synchronized (sPool) {
            if (sPool.size() < MAX_POOLED_HINT) {
                sPool.add(this);
            }
        }
    }

    public static void clearPool() {
        synchronized (sPool) {
            sPool.clear();
        }
    }

    public Bitmap getBitmap() {
        if (sConfigLowMemoryMode || DevicesUtils.lowMemoryDevices()) {
            if (bitmapRef != null) {
                return bitmapRef.get();
            }
        } else {
            return bitmap;
        }
        return null;
    }

    public void setBitmap(Bitmap bmp) {
        if (sConfigLowMemoryMode || DevicesUtils.lowMemoryDevices()) {
            bitmapRef = new SoftReference<Bitmap>(bmp);
        } else {
            bitmap = bmp;
        }
    }
}
